<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Sys extends Controller_Admin_Base {
    
    public function before()
    {
        parent::before();
        if ($this->auto_render)
        {
            $this->template->ptitle="系统管理";
        }
    }
    //系统首页
    public function action_index()
    {
        $this->template->title = __('首页');

        $model=new Model_Admin_Post();

        $postList=$model->getNewInfo();

        $view=View::factory('admin/sys/index');

        $view->postList=$postList;
        
        $this->template->content = $view;
        
    }
    //用户管理
    public function action_user()
    {
        $model=new Model_Admin_SysUser();

        $name=$this->request->query('name');

        $email=$this->request->query('email');

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $userList=$model->getInfo($name,$email,$offset,$pageSize);

        $pages=ceil((intval($userList['total']))/$pageSize);

        $this->template->title = __('用户管理');

        $view = View::factory('admin/sys/user');

        $view->userList =$userList;

        $view->page= $page;

        $view->pages=$pages;

        $view->name=$name;

        $view->email=$email;

        $this->template->content =  $view;
    }

    //模块管理
    public  function action_model()
    {
        $model=new Model_Admin_SysModel();

        $name=$this->request->query('name');

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $modelList=$model->getInfo($name,$offset,$pageSize);

        $pages=ceil((intval($modelList['total']))/$pageSize);

        $this->template->title = __('模块管理');

        $view = View::factory('admin/sys/model');

        $view->modelList =$modelList;

        $view->page= $page;

        $view->pages=$pages;

        $view->name=$name;

        $this->template->content =  $view;
    }
    //用户组管理
    public  function action_users()
    {
        $model=new Model_Admin_Users();

        $name=$this->request->query('name');

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $usersList=$model->getInfo($name,$offset,$pageSize);

        $pages=ceil((intval($usersList['total']))/$pageSize);

        $this->template->title = __('用户组管理');

        $view = View::factory('admin/sys/users');

        $view->usersList =$usersList;

        $view->page= $page;

        $view->pages=$pages;

        $view->name=$name;
        
        $this->template->content =  $view;
    }
    //注销
    public function  action_logout()
    {

        Session::instance()->get('admin_username','');

        $this->redirect('/admin/home/index');
    }
    //产品属性设置
    public function action_attributes(){

        $model=new Model_Admin_Property();

        $name=$this->request->query('caption');

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $attributesList=$model->getInfo($name,$offset,$pageSize);

        $pages=ceil((intval($attributesList['total']))/$pageSize);

        $this->template->title = __('产品属性管理');

        $view = View::factory('admin/sys/attributes');

        $view->attributesList =$attributesList;

        $view->page= $page;

        $view->pages=$pages;

        $view->caption=$name;
        
        $this->template->content =  $view;
    }
     //用户管理
    public function action_setGroupUser()
    {

        $usersid=$this->request->query("usersid");

        $model=new Model_Admin_SysUser();

        $name=$this->request->query('name');

        $email=$this->request->query('email');

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $userList=$model->getUserByUsersid($usersid,$name,$email,$offset,$pageSize);

        $pages=ceil((intval($userList['total']))/$pageSize);

        $this->template->title = __('用户管理');

        $view = View::factory('admin/sys/setGroupUser');

        $view->userList =$userList;

        $view->page= $page;

        $view->pages=$pages;

        $view->name=$name;

        $view->email=$name;

        $view->usersid=$usersid;

        $this->template->content =  $view;

    }

    public function action_alertPwd(){

        $this->template->title = __('修改密码');

        $view = View::factory('admin/sys/alertPwd');
        
        $this->template->content =  $view;
    }

    public function action_log(){

        $log_model=new Model_Admin_SysLog();

        $type=$this->request->query('type');

        $start_date=$this->request->query('start_date');

        $end_date=$this->request->query('end_date');

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $logList=$log_model->getInfo($type,$start_date,$end_date,$offset,$pageSize);

        $pages=ceil((intval($logList['total']))/$pageSize);

        $this->template->title = __('日志信息');

        $view = View::factory('admin/sys/log');

        $view->logList =$logList;

        $view->page= $page;

        $view->pages=$pages;

        $view->type=$type;

        $this->template->content =  $view;
    }
    public function action_setPwd(){
        try {
            $userid=Session::instance()->get('admin_userid');
            $pwd=sha1(md5('123456'));
            $total_rows=DB::update('sysuser')->set(array('loginpwd'=>$pwd))
                ->where('sysuserid','=',$userid)->execute();
            Database::instance()->commit();
            return json_encode(array("info"=>"success","count"=>$total_rows));
        }
        catch (Exception $e) {
            Database::instance()->rollback();
            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
}
